package bb.lanxing.manager;

import static android.content.Context.AUDIO_SERVICE;

import android.app.Activity;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import bb.lanxing.App;
import bb.lanxing.R;

public class CadenceTrainingManager {
    private static CadenceTrainingManager instance;
    private static boolean isTraining;
    private ScheduledFuture future;
    private int soundId;
    private SoundPool soundPool;
    private Runnable task;
    private ScheduledExecutorService timer;
    private int trainingCadence;

    public static boolean isTraining() {
        return isTraining;
    }

    public static CadenceTrainingManager getInstance() {
        if (instance == null) {
            instance = new CadenceTrainingManager();
        }
        return instance;
    }

    private CadenceTrainingManager() {
        App context = App.getContext();
        if (Build.VERSION.SDK_INT < 21) {
            this.soundPool = new SoundPool(1, 3, 0);
        } else {
            this.soundPool = new SoundPool.Builder().setMaxStreams(1).setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(3).setContentType(2).setUsage(1).build()).build();
        }
        this.soundId = this.soundPool.load(context, R.raw.beep, 1);
        AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
        if (audioManager.getStreamVolume(3) == 0) {
            audioManager.setStreamVolume(3, 5, 0);
        }
        this.trainingCadence = SharedManager.getInstance().getPersonalCadence();
        this.task = new Task();
        this.timer = Executors.newSingleThreadScheduledExecutor();
    }

    public void start(Activity activity, int i) {
        if (!isTraining) {
            if (i == 0) {
                i = 80;
            }
            this.trainingCadence = i;
            activity.setVolumeControlStream(3);
            this.future = this.timer.scheduleAtFixedRate(this.task, 1000L, 60000 / this.trainingCadence, TimeUnit.MILLISECONDS);
            isTraining = true;
            return;
        }
        changeCadence(i);
    }

    public void changeCadence(int i) {
        this.trainingCadence = i;
        SharedManager.getInstance().setPersonalCadence(this.trainingCadence);
        if (isTraining) {
            this.future.cancel(true);
            this.future = this.timer.scheduleAtFixedRate(this.task, 0L, 60000 / this.trainingCadence, TimeUnit.MILLISECONDS);
        }
    }

    public void stop() {
        if (isTraining) {
            this.future.cancel(true);
            isTraining = false;
        }
    }

    private void destroy() {
        try {
            this.timer.shutdown();
            this.soundPool.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void release() {
        CadenceTrainingManager cadenceTrainingManager = instance;
        if (cadenceTrainingManager != null) {
            cadenceTrainingManager.destroy();
            isTraining = false;
            instance = null;
        }
    }

    private class Task implements Runnable {
        private Task() {
        }

        @Override
        public void run() {
            try {
                CadenceTrainingManager.this.soundPool.play(CadenceTrainingManager.this.soundId, 1.0f, 1.0f, 1, 0, 1.0f);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}